<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use App\GoodStore;
use App\GoodStoreMember;
use Validator;
use App\GoodClass;
use App\GoodDefaultClass;
use App\Good;
use App\GoodColorSize;
use App\GoodColor;
use App\GoodUserDefine;

class GoodClassController extends Controller
{
    //
    public function create(Request $request) {
        $validator = Validator::make($request->all(), array(
            'name' => 'required|max:255',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = DB::table('good_store_members')
            ->where('memberId', $request->userId)
            ->first();

        $class = new GoodClass();
        $class->name = $request->name;
        $class->storeId = $member->storeId;
        $class->description = $member->description;
        $class->save();

        $this->returnEcho();
    }

    public function edit(Request $request) {
        $validator = Validator::make($request->all(), array(
            'classId' => 'required',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        if ($request->classId == 0) {
            $class = GoodDefaultClass::find(1);
            $class->name = $request->name;
            $class->description = $request->description;
            $class->save();
            $this->returnEcho();
            return ;
        }
        
        $class = GoodClass::find($request->classId);
        if ($class == null) {
            $this->returnEcho(403, 'class not found', '');
            return ;
        }

        $class->name = $request->name;
        $class->description = $request->description;
        $class->save();
        $this->returnEcho();
        return ;
    }


    public function delete(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255',
            'classId' => 'required|integer'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $class = GoodClass::find($request->classId);
        $goods = Good::where('classId', $request->classId)->get();
        foreach ($goods as $good) {
            $good->classId = 0;
            $good->save();
        }
        $class->delete();

        $this->returnEcho();
    }

    public function findClasses(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = DB::table('good_store_members')
            ->where('memberId', $request->userId)
            ->first();

        $classes = GoodClass::where('storeId', $member->storeId)->get();

        $data = [];
        $picture = '';
        $goods = Good::where('classId', 0)->where('storeId', $member->storeId)->get();
        foreach($goods as $good) {
            $color = GoodColor::where('goodId', $good->id)->first();
            if ($color != null) {
                $picture = $color->picture;
            }
        }
        $description = '';
        $des = GoodDefaultClass::where('storeId', $member->storeId)->first();
        if ($des != null)
            $description = $des->description;
        array_push($data, ["classId"=>strval(0), "name"=>"默认分类", "number"=>count($goods), "picture"=> $picture, "description" => $description]);
        foreach($classes as $class)
        {
            $picture = '';
            $goods = Good::where('classId', $class->id)->get();
            foreach($goods as $good) {
                $color = GoodColor::where('goodId', $good->id)->first();
                if ($color != null) {
                    $picture = $color->picture;
                }
            }
            $description = '';
            if ($class->description != null)
                $description = $class->description;
            array_push($data, ["classId" => strval($class->id), "name" => $class->name, "number" => count($goods), "picture" => $picture, "description" => $description]);
        }
        echo json_encode(['status' => 200, 'errmsg' => '', 'data' => $data]);

    }
}
